import { useIntersectionObserver, useIntervalFn } from '@vueuse/core'
import { ref, computed } from 'vue'
import dayjs from 'dayjs'

export function vueuse (target, func) {
  const { stop } = useIntersectionObserver(
    target,
    ([{ isIntersecting }], observerElement) => {
      if (isIntersecting) {
        func()
        stop()
      }
    }
  )
}

export function timeOut (val) {
  const count = ref(0)
  const countTimeText = computed(() => {
    return dayjs.unix(count.value).format('mm分ss秒')
  })
  const { pause, resume } = useIntervalFn(() => {
    /* your function */
    if (count.value <= 0) {
      return pause()
    }
    count.value--
  }, 1000)
  const start = (time) => {
    count.value = time || val
    resume()
  }
  return { count, start, pause, resume, countTimeText }
}
